/*
 * File     : AbstractStructureFactory.java
 * Created  : 24 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.structure;

import com.googlecode.dni.internal.DniInternal;
import com.googlecode.dni.type.LifetimeManagedList;
import com.googlecode.dni.type.NativeObjectList;
import com.googlecode.dni.type.Pointer;
import com.googlecode.dni.type.structure.Structure;
import com.googlecode.dni.type.structure.StructureFactory;

/**
 * <p>
 *  Provides the base functionality for {@link Structure} types.
 * </p>
 *
 * @param <T>
 *            the type of structure
 *
 * @author Matthew Wilson
 */
public abstract class AbstractStructureFactory< T extends Structure >
    implements StructureFactory< T >
{

    /** The structure interface type. */
    private final Class< T > structureType;


    /**
     * @param structureType
     *            the structure interface type
     */
    protected AbstractStructureFactory( final Class< T > structureType )
    {
        this.structureType = structureType;
    }

    /**
     * Creates from a pointer; utility method for use by implementations to save
     * writing out the same byte-code over and over again which makes my brain
     * hurt right now.
     *
     * @param pointer
     *            the pointer
     *
     * @return the structure
     */
    public final T fromPointer( final long pointer )
    {
        return fromPointer( new Pointer( pointer ) );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final NativeObjectList< T > allocateList( final int size )
    {
        StructureRecord record = listPreamble( size );
        return new StructureList< T >( this.structureType, this, record.getInfo().getSize(), size );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final LifetimeManagedList< T > allocateLifetimeManagedList( final int size )
    {
        StructureRecord record = listPreamble( size );
        return new StructureLifetimeManagedList< T >( this.structureType,
                                                      this,
                                                      record.getInfo().getSize(),
                                                      size );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public NativeObjectList< T > listFromPointer( final Pointer pointer, final int size )
    {
        StructureRecord record = listPreamble( size );
        return new StructureList< T >( pointer,
                                       this.structureType,
                                       this,
                                       record.getInfo().getSize(), size );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public LifetimeManagedList< T > lifetimeManagedListFromPointer( final Pointer pointer, final int size )
    {
        StructureRecord record = listPreamble( size );
        return new StructureLifetimeManagedList< T >( pointer,
                                                      this.structureType,
                                                      this,
                                                      record.getInfo().getSize(), size );
    }

    /**
     * Creates a new instance using allocated memory.
     *
     * @return a new instance
     */
    protected abstract Structure allocateUntyped();

    /**
     * Creates a new instance from a pointer.
     *
     * @param pointer
     *            the existing pointer
     *
     * @return a new instance
     */
    protected abstract Structure fromPointerUntyped( long pointer );


    /**
     * Pre-amble for the list factory methods.
     *
     * @param size
     *            the number of elements (for checking)
     * @return the structure record
     */
    private StructureRecord listPreamble( final int size )
    {
        if ( size < 0 )
        {
            throw new IllegalArgumentException( "Size: " + size );
        }

        // TODO at some point, if size is "big", use a sparse impl

        StructureRecord record = DniInternal.getStructureRecord( this.structureType );
        if ( record.getInfo().getSize() < 0 )
        {
            throw new UnsupportedOperationException( "Opaque structure: " + this.structureType.getName() );
        }
        return record;
    }

}
